package ws

import (
	"net/http"

	"github.com/gorilla/websocket"
	. "starsine.cn/appTpl/cmn/defs"
	"starsine.cn/appTpl/cmn/logs"
	. "starsine.cn/appTpl/svccenter/registry/restful/ws-server/wsserver"
)

var wsMng *WsMng = nil

func Start(appCtxt *AppContext) {
	wsMng = CreateWsMng(appCtxt)

	wsMng.Start()

	logs.Infof("registry.wsserver start")
}

func Dump() string {
	if wsMng == nil {
		return "wsMng not init"
	}

	return wsMng.Dump()
}

func Broadcast(v interface{}) {
	if wsMng == nil {
		logs.Errorf("wsMng not init")
		return
	}

	wsMng.wsBroadcast(v)
}

func Send(conn *websocket.Conn, v interface{}) {
	if wsMng == nil {
		logs.Errorf("wsMng not init")
		return
	}

	wsMng.wsSend(conn, v)
}

func WSHandler(resp http.ResponseWriter, req *http.Request) *WsServer {
	if wsMng == nil {
		logs.Errorf("wsMng not init")
		return nil
	}

	return wsMng.wsHandler(resp, req)
}
